<!--
 * @Author: weikang
 * @Description:
 * @Date: 2023-12-22 22:47:46
 * @LastEditTime: 2025-04-09 21:51:50
 * @FilePath: \env-manager-pc\src\views\assets\demolish\index.vue
-->
<script setup lang="ts">
import { ElMessage } from 'element-plus'
import detail from '../statistics/detail.vue'
import edit from './edit.vue'
import type { InvalidForm } from './page.ts'
import { useAssetModelClassifys, useOwnerOrgs, useStateTypes } from '@/utils/dataHook'
import { QueryObj } from '@/utils/dataHook/data'
import { usePage } from '@/utils/commonHook'
import assetsApi from '@/api/modules/assets'
import { ModelType } from '@/types/common'
import type { doLoading } from '@/types/page'
import type { orderForm } from '@/api/modules/types/assets'

const searchData = reactive(new QueryObj({
  objType: 'DIM.FilterCollection',
  Filters: [{
    Type: 'DIM.GYHR.OrderFormFilters.DateFilter',
    FilterKey: 'PlaceDate',
    valueKey: 'PlaceDate',
  }, {
    Type: 'DIM.GYHR.OrderFormFilters.AssetModelFilter',
    FilterKey: 'AssetFactory',
    valueKey: 'AssetFactory',
  },
  {
    Type: 'DIM.GYHR.OrderFormFilters.OrderFormStrFilter',
    FilterKey: 'Recipient',
    valueKey: 'Recipient',
  },
  {
    Type: 'DIM.Business.AssetFilters.EntNameFilter',
    FilterKey: 'Enterprise',
    valueKey: 'Enterprise',
    mutlip: true,
  },
  {
    Type: 'DIM.Assets.Filters.AssetStateFilter',
    FilterKey: '',
    valueKey: 'DIM.Assets.Filters.AssetStateFilter',
    mutlip: true,
  },
  {
    Type: 'DIM.Assets.Filters.AssetModelFilter',
    FilterKey: '',
    valueKey: 'DIM.Assets.Filters.AssetModelFilter',
    mutlip: true,
  },
  {
    Type: 'DIM.Assets.Filters.AssetCodeFilter',
    FilterKey: '',
    valueKey: 'DIM.Assets.Filters.AssetCodeFilter',
    mutlip: true,
  },
  {
    Type: 'DIM.GYHR.AssetFilters.OrderFormIDFilter',
    FilterKey: 'OFID',
    valueKey: 'OFID',
  },
  {
    Type: 'DIM.Assets.Filters.AssetModelClassifyFilter',
    FilterKey: '',
    valueKey: 'DIM.Assets.Filters.AssetModelClassifyFilter',
    mutlip: true,
  },
  ],
}))
const { page, pageNumChange, pageSizeChange } = usePage(getTableData)

const stateTypes = useStateTypes()
const ModelClassifys = useAssetModelClassifys()
// const AssetModels = useAssetModels()
const OwnerList = useOwnerOrgs()
const defaultProps = {
  children: 'Children',
  label: 'Name',
}
const editRef = ref()

function search() {
  page.PageNum = 1
  getTableData()
}
const tableData = ref<orderForm[]>([])
const loading = ref(false)

async function getTableData() {
  loading.value = true
  const res = await assetsApi.GetAssets({
    objFilter: searchData.getQuery(),
    objType: searchData.objType,
    pageSize: page.PageSize,
    pageNum: page.PageNum,
  })
  loading.value = false
  const { Rows, RecordsetCount } = res
  tableData.value = Rows || []
  page.RecordsetCount = RecordsetCount
}
const detailRef = ref()
function toDetail(row: orderForm) {
  detailRef.value.open({ type: ModelType.edit, data: row })
}

const tableRef = ref()
// function toBatch() {
//   const rows = tableRef.value.getCheckboxRecords()
//   if (!rows.length) {
//     return ElMessage.warning('请选择要拆除的资产')
//   }
//   const assetObjIDs = rows.map((item: orderInfo) => item.AssetObjID)
//   editRef.value.open({ type: OperateType.batch, data: { ...rows[0], assetObjIDs } })
// }
// function toEdit(row: orderForm) {
//   editRef.value.open({ type: ModelType.edit, data: row })
// }

getTableData()

async function submitDemolish(formData: InvalidForm, doLoading: doLoading) {
  try {
    doLoading(true)
    await assetsApi.ChangeInvalid(formData)
    getTableData()
    editRef.value.close()
    ElMessage.success({
      message: '拆除成功',
      type: 'success',
    })
  }
  catch (e) {
    doLoading(false)
  }
}
</script>

<template>
  <div>
    <PageHeader>
      <template #content>
        <ElForm label-width="100">
          <ElRow>
            <ElCol :span="6">
              <ElFormItem label="订单号">
                <ElInput v-model="searchData.OFID" placeholder="订单号" clearable />
              </ElFormItem>
            </ElCol>
            <ElCol :span="6">
              <ElFormItem label="安装日期">
                <ElDatePicker v-model="searchData.ContractIncomeTime" value-format="YYYYMMDD" type="daterange" />
              </ElFormItem>
            </ElCol>
            <ElCol :span="6">
              <ElFormItem label="产品名称">
                <ElSelect v-model="searchData['DIM.Assets.Filters.AssetModelClassifyFilter']" class="w-full" clearable>
                  <ElOption v-for="item in ModelClassifys" :key="item.ObjID" :label="item.Name" :value="item.ObjID" />
                </ElSelect>
              </ElFormItem>
            </ElCol>
            <ElCol :span="6">
              <ElFormItem label="资产状态">
                <ElSelect v-model="searchData['DIM.Assets.Filters.AssetStateFilter']" class="w-full" clearable>
                  <ElOption v-for="item in stateTypes" :key="item.StateID" :label="item.Name" :value="item.StateID" />
                </ElSelect>
              </ElFormItem>
            </ElCol>
            <ElCol :span="6">
              <ElFormItem label="设备编码">
                <ElInput v-model="searchData['DIM.Assets.Filters.AssetCodeFilter']" clearable />
              </ElFormItem>
            </ElCol>
            <ElCol :span="6">
              <ElFormItem label="运维公司">
                <el-tree-select
                  v-model="searchData['DIM.GYHR.ContractFilters.OwnerOrgGUIDFilter']"
                  value-key="Code"
                  class="w-full"
                  :data="OwnerList"

                  :render-after-expand="false"
                  :props="defaultProps"
                  clearable check-strictly
                />
              </ElFormItem>
            </ElCol>
            <ElCol :span="6">
              <ElFormItem label="企业名称">
                <ElInput v-model="searchData.Enterprise" clearable />
              </ElFormItem>
            </ElCol>
            <ElCol :span="6" class="flex flex-justify-end">
              <ElButton type="primary" @click="search">
                查询
              </ElButton>
              <ElButton type="warning">
                导出
              </ElButton>
            </ElCol>
          </ElRow>
        </ElForm>
      </template>
    </PageHeader>
    <PageMain>
      <div v-auto-height="85">
        <vxe-table
          ref="tableRef"
          auto-resize border
          height="100%"
          :data="tableData"
          :loading="loading"
          :row-config="{ isCurrent: true }"
        >
          <VxeColumn type="checkbox" width="80" />
          <VxeColumn type="seq" title="序号" width="80" />
          <VxeColumn field="OrderFormCode" title="订单号" min-width="150" />
          <VxeColumn field="OwnerOrgName" title="运维公司" width="140" />
          <VxeColumn field="AssetModelClassifyName" title="产品名称" min-width="150" />
          <VxeColumn field="AssetModelName" title="型号" width="150" />
          <VxeColumn field="OrderFormCode" title="设备编码" width="150">
            <template #default="{ row }">
              <el-button type="primary" link @click="toDetail(row)">
                {{ row.AssetCode }}
              </el-button>
            </template>
          </VxeColumn>
          <VxeColumn field="StateName" title="资产状态" width="140" />
          <VxeColumn field="InvalidAccount" title="拆除人" width="120" />
          <VxeColumn field="InvalidTime" title="拆除日期" width="150" />
        </vxe-table>
      </div>
      <ElPagination
        class="mt-4"
        :page-size="page.PageSize"
        :total="page.RecordsetCount"
        :current-page="page.PageNum"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[20, 50, 100]"
        @current-change="pageNumChange"
        @size-change="pageSizeChange"
      />
      <detail ref="detailRef" />
      <edit ref="editRef" @submit="submitDemolish" />
    </PageMain>
  </div>
</template>
